package com.example.threadPool;

import java.util.concurrent.*;

public class Tpool4 {
    public static void main(String[] args) {
        ExecutorService executor = new ThreadPoolExecutor(
                2, // 核心线程数
                4, // 最大线程数
                60, TimeUnit.SECONDS, // 非核心线程空闲回收时间
                new ArrayBlockingQueue<>(2), // 任务队列容量为2
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy() // 拒绝策略：抛异常
        );

        for (int i = 1; i <= 10; i++) {
            final int taskId = i;
            executor.execute(() -> {
                System.out.println("任务 " + taskId + " 由 " + Thread.currentThread().getName() + " 执行");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        executor.shutdown();
    }
}
